package com.b1502.store2.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.b1502.store2.R;
import com.b1502.store2.adapter.NewsItemAdapter;
import com.b1502.store2.model.InformationData;
import com.b1502.store2.model.NewsDetail;
import com.b1502.store2.model.StoreParams;
import com.b1502.store2.util.UrlUtil;
import com.google.gson.Gson;

import org.xmlpull.v1.XmlPullParser;
import org.xutils.common.Callback;
import org.xutils.x;

public class NewsDetailsActivity extends BaseActivity {

    private WebView webview_activity_news;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_details);
        init();
    }

    private void init() {
        initStoreBar("资讯详情");
        webview_activity_news = (WebView) findViewById(R.id.webview_activity_news);
        WebSettings webSettings = webview_activity_news.getSettings();
        webSettings.setJavaScriptEnabled(true);
        String imageId = getIntent().getStringExtra("imageId");
        getNewsItems(imageId);
    }

    private void getNewsItems(String imageId) {
        StoreParams params = new StoreParams(UrlUtil.GetNews,"newsId",imageId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                NewsDetail newsDetail = gson.fromJson(result,NewsDetail.class);
                webview_activity_news.loadDataWithBaseURL(null, newsDetail.Content, "text/html", "utf-8", null);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(NewsDetailsActivity.this,"网络请求失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
